package com.xyx.controller.product;

import com.github.pagehelper.PageInfo;
import com.xyx.entity.Result;
import com.xyx.pojo.Product;
import com.xyx.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/product")
public class productController {
    @Autowired
    private IProductService productService;
    /**
     * 产品列表查询
     * @param productName
     * @param model
     * @return
     */
    @RequestMapping("/product_list")
    public  String product_list(String productName, Model model ,
    @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
    @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
    List<Product> allProducts = productService.getAllProducts(productName, pageNum, pageSize);
    PageInfo<Object> pageInfo = new PageInfo<>(allProducts);
        // System.out.println(pageInfo);
        model.addAttribute("product_list",allProducts);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("productName",productName);
        return "product_list";
    }
    /**
     * 删除产品
     */
    @RequestMapping("delProduct")
    @ResponseBody
    public Result deleteProductById(Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            int i=productService.deleteProductById(id);
            if(i>0){
                System.out.println("删除成功");
                result.setMsg("删除成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("删除失败");
                result.setMsg("删除失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择要删除的ID！");
            result.setMsg("请选择要删除的ID");
            result.setSuccess(false);
            return   result;
        }
    }
    /**
     * 批量删除产品
     */
    @RequestMapping("delAllProduct")
    @ResponseBody
    public Result deleteAllProduct(String idps) {
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(idps);
        if(idps!=null && !idps.trim().equals("")){

            String[] strArray = idps.split(",");
            List<String> idpList = Arrays.asList(strArray);

            int i = productService.deleteAllProduct(idpList);
            if(i>0){
                System.out.println("删除成功");
                result.setMsg("删除成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("删除失败");
                result.setMsg("删除失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择数据！");
            result.setMsg("请选择数据");
            result.setSuccess(false);
            return   result;
        }
    }


    /**
     * 新增产品
     */
    @RequestMapping("/addProduct")
    @ResponseBody
    public Result addProduct(Product product){
        System.out.println(product);
        Result<Object> result=new Result<>();
        if(product!=null
                && product.getProductName()!=null
                && !product.getProductName().trim().equals("")
        ){
            System.out.println(product);
            Product product1 = productService.getProductByproductName(product.getProductName());
            if(product1!=null){
                result.setMsg("用户已经存在");
                result.setSuccess(false);
                return   result;

            }
            int i = productService.addProduct(product);
            if(i>0){
                System.out.println("新增成功");
                result.setMsg("新增成功");
                result.setSuccess(true);
                return   result;
            }else {
                System.out.println("新增失败");
                result.setMsg("新增失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请输入产品名！");
            result.setMsg("请输入产品名");
            result.setSuccess(false);
            return   result;
        }

    }
    @RequestMapping("/getProductById")
    @ResponseBody
    public Result getProductById(Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            Product product = productService.getProductById(id);

            if(product!=null){
                result.setData(product);
                System.out.println("查找成功");
                result.setMsg("查找成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("没有此用户");
                result.setMsg("没有此用户");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择ID！");
            result.setMsg("请选择ID");
            result.setSuccess(false);
            return   result;
        }

    }
    /**
     * 编辑用户
     */
    @RequestMapping("/editProduct")
    @ResponseBody
    public Result editProduct(Product product){
        System.out.println(product);
        Result<Object> result=new Result<>();
        if(product!=null
                && product.getProductName()!=null
                && !product.getProductName().trim().equals("")
                && product.getId()!=null
        ){
            System.out.println(product);
            int i = productService.editProduct(product);
            if(i>0){
                System.out.println("修改成功");
                result.setMsg("修改成功");
                result.setSuccess(true);
                return   result;
            }else {
                System.out.println("修改失败");
                result.setMsg("修改失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请输入用户名和ID！");
            result.setMsg("请输入用户名和ID");
            result.setSuccess(false);
            return   result;
        }
    }
}
